/**
 * 服务器AI管理
 */
class serverAIMgr {
	private static instance: serverAIMgr;
	public static getInstance(): serverAIMgr {
		if (this.instance == null) {
			this.instance = new serverAIMgr();
		}
		return this.instance;
	}

	public constructor() {
	}
	/**AI池子 */
	public AIPools = [];
	/**AI数量 */
	public totalAIPool = 0;
	/**玩家管理 */
	private userMgr = AIUserMgr.getInstance();

	public c = serverConfig.config;

	/**
	 * 用来清空AI的状态
	 * @param {*} number 
	 */
	public resetAI = function (player) {
		if (player != null) {
			if (!player.isAI) {
				console.log("resetAI 玩家不是AI");
				return;
			}
			else {
				this.AIPools[player.userId].isUsed = false;
				this.AIPools[player.userId].roomId = -1;
				this.AIPools[player.userId].otherLineUser = -1;
				this.AIPools[player.userId].speed = this.c.initUserSpeed;
				player.roomId = -1;
				this.userMgr.resetPlayer(player);
			}
		}
		else {
			console.log("resetAI 玩家不存在");
		}
	}
	/**制造AI */
	public creatAIs(number) {
		if (this.AIPools.length >= number)
			return;
		number -= this.AIPools.length;
		for (var i = 0; i < number; i++) {
			this.createAI(i);
		}
	}
	/**遍历所有AI初始化 */
	private cleanRoomAI() {
		for (var i = 0; i < this.AIPools.length; ++i) {
			this.resetAI(this.AIPools[i]);
		}
	}
	/**初始化AI入口 */
	public restAIs() {
		this.cleanRoomAI();
	}
    /**
	 * 创建一个ai
	 */
	public createAI(tuserId) {
		var currentPlayer;
		let who = Math.floor(Math.random() * 3);
		who = 0;
		switch (who) {
			case 0:
				currentPlayer = new AI_base(tuserId);
				break;
			default:
				break;
		}

		this.AIPools[tuserId] = currentPlayer;
		AIUserMgr.getInstance().addAI(currentPlayer);


		//console.log("创建AI成功");
		this.totalAIPool++;
	}
	/**获取AI */
	public getAI() {

		//获取当前没有使用的AI
		for (var i = 0; i < this.AIPools.length; ++i) {
			if (!this.AIPools[i].isUsed) {
				this.resetAI(this.AIPools[i]);
				this.AIPools[i].isUsed = true;
				return this.AIPools[i];
			}
			else {
				// console.log("已经使用过的AI"+i);
			}
		}
	}
	/**
	 * AI 思考 移动
	 */
	public AIUpdate() {

		for (var i = 0; i < this.AIPools.length; ++i) {
			this.AIPools[i].AIUpdate();
		}
	}
	/**获取AI数量 */
	public gettotalAIPools() {
		return this.totalAIPool;
	};
}
